package dr.joketalker.dao.json;

import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import dr.joketalker.dao.FactoryDAO;
import dr.joketalker.dao.JokeEvaluationDAO;
import dr.joketalker.entity.JokeEvaluation;
import dr.joketalker.entity.User;

public class JSONJokeEvaluation extends JSONGeneric<JokeEvaluation> implements JokeEvaluationDAO{

	static final String USER_ID 		= "USER_ID";
	static final String JOKE_ID 		= "JOKE_ID";
	static final String POINTS   		= "POINTS";
	static final String COMMENT   		= "COMMENT";
	
	
	@Override
	public List<NameValuePair> getListParams(JokeEvaluation eval) {
		List<NameValuePair> listParams = super.getListParams(eval);
		
		listParams.add(new BasicNameValuePair(USER_ID, eval.getUser().getId()+""));		
		listParams.add(new BasicNameValuePair(JOKE_ID, eval.getJoke().getId()+""));
		listParams.add(new BasicNameValuePair(POINTS, eval.getPoints()+""));
		listParams.add(new BasicNameValuePair(COMMENT, eval.getComment()+""));
		
		
		return listParams;
	}
	
	@Override
	public JokeEvaluation buildEntityFromJson(JSONObject jsonO) {
		JokeEvaluation eval = new JokeEvaluation();
		try{
			eval.setId(jsonO.getLong(ID));
			eval.setPoints(new Short(jsonO.getString(POINTS)));
			
			long userId			= jsonO.getLong(USER_ID);
			
			User cUser = FactoryDAO.getUserDao().read(userId);
			eval.setUser(cUser);
			
			eval.setComment(jsonO.getString(COMMENT));
			

		}catch(Exception ex){
			
		}
		return eval;
	}


	@Override
	public JokeEvaluation getEntityInstance() {
		// TODO Auto-generated method stub
		return new JokeEvaluation();
	}

}
